home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 1.iso
/
toolbox
/
src
/
exampleCode
/
opengl
/
defrasterPort
/
defo.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-11-11
|
7KB
|
301 lines
/*
* Copyright (c) 1994, Silicon Graphics, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that the name of Silicon Graphics may not be used in any advertising or
* publicity relating to the software without the specific, prior written
* permission of Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE
* POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* OpenGL(TM) is a trademark of Silicon Graphics, Inc.
*/
/*
* Example to convert (and render) a font defined by defrasterfont()
* in irisGL to an openGL bitmap
*
* Yusuf Attarwala
*
* cc defo.c -o defo -lGL -lGLw -lX11 -lXt -lXm
*
*/
#include <X11/Xlib.h>
#include <Xm/Form.h>
#include <Xm/Frame.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/GLwMDrawA.h>
int test_width = 33;
int test_height = 33;
unsigned short test_raster[] = {
0x000f, 0xf000, 0x0000,
0x0010, 0x0800, 0x0000,
0x0060, 0x0600, 0x0000,
0x0180, 0x0180, 0x0000,
0x0600, 0x0060, 0x0000,
0x0800, 0x0010, 0x0000,
0x0803, 0xc010, 0x0000,
0x100f, 0xf008, 0x0000,
0x1038, 0x1c08, 0x0000,
0x2060, 0x0604, 0x0000,
0x2080, 0x0104, 0x0000,
0x4000, 0x0002, 0x0000,
0x8000, 0x0001, 0x0000,
0x8003, 0xc001, 0x0000,
0x8003, 0xc001, 0x0000,
0x8001, 0x8001, 0x0000,
0x8000, 0x0001, 0x0000,
0x8000, 0x0001, 0x0000,
0x8070, 0x0e01, 0x0000,
0x8070, 0x0e01, 0x0000,
0x8070, 0x0e01, 0x0000,
0x4070, 0x0e02, 0x0000,
0x2000, 0x0004, 0x0000,
0x2000, 0x0004, 0x0000,
0x1000, 0x0008, 0x0000,
0x1000, 0x0008, 0x0000,
0x0800, 0x0010, 0x0000,
0x0800, 0x0010, 0x0000,
0x0600, 0x0060, 0x0000,
0x01ff, 0xff80, 0x0000,
0x007f, 0xfe00, 0x0000,
0x001f, 0xf800, 0x0000,
0x000f, 0xf000, 0x0000};
GLubyte *defToO();
#define BITUNIT 8
int width,height;
Display *display;
XtAppContext appContext;
XVisualInfo *vi;
Widget toplevel,glw;
GLXContext glxc;
GLubyte *stip;
Arg args[20];
int acnt;
static void createToplevel(),
drawScene(),
setMatrix(),
exposeCB(),
resizeCB(),
initCB();
void
testBitmap(raster)
GLubyte *raster;
{
stip = raster;
createToplevel(); /* create and realize widget hierarchy */
XtAppMainLoop(appContext); /* loop for ever */
}
void
createToplevel(void)
{
Widget frame;
acnt = 0;
XtSetArg(args[acnt],XmNminHeight, 200);acnt++;
XtSetArg(args[acnt],XmNminWidth, 200);acnt++;
toplevel = XtAppCreateShell("openGL bitmap","obit",
applicationShellWidgetClass,
display,args,acnt);
/* create a frame to hold the glx drawing area */
frame = XtVaCreateManagedWidget("frame",
xmFrameWidgetClass, toplevel,
NULL);
/* create and manage a single buffer widget, rgb mode */
acnt = 0;
XtSetArg(args[acnt], GLwNrgba, TRUE); acnt++;
glw = GLwCreateMDrawingArea(frame, "glw", args, acnt);
XtManageChild(glw);
/* register callbacks */
XtAddCallback(glw, GLwNginitCallback, initCB, NULL);
/* realize widget hierarchy */
XtRealizeWidget(toplevel);
/* additional callbacks */
XtAddCallback(glw, GLwNresizeCallback, resizeCB, NULL);
XtAddCallback(glw, GLwNexposeCallback, exposeCB, NULL);
}
void
drawScene()
{
glClearColor(0.8, 0.8, 0.8, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,0.0);
glRasterPos2f(20.5,20.5);
glBitmap(width,height,0.0,0.0,width+2,0.0,stip);
glFlush();
}
static void
setMatrix()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,100.0,0.0,100.0,-1.0,1.0); /* a simple ortho */
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); /* default view */
}
static void
resizeCB(w,client_data,call)
Widget w;
XtPointer client_data;
XtPointer call;
{
GLwDrawingAreaCallbackStruct *call_data;
call_data = (GLwDrawingAreaCallbackStruct *) call;
GLwDrawingAreaMakeCurrent(w, glxc);
glViewport(0, 0, call_data->width, call_data->height);
setMatrix();
drawScene();
}
static void
exposeCB(w,client_data,call)
Widget w;
XtPointer client_data;
XtPointer call;
{
GLwDrawingAreaCallbackStruct *call_data;
call_data = (GLwDrawingAreaCallbackStruct *) call;
GLwDrawingAreaMakeCurrent(w, glxc);
drawScene();
}
static void
initCB(w,client_data,call)
Widget w;
XtPointer client_data;
XtPointer call;
{
XVisualInfo *vi;
XtSetArg(args[0], GLwNvisualInfo, &vi);
XtGetValues(w, args, 1);
glxc = glXCreateContext(XtDisplay(w), vi, 0, GL_TRUE);
GLwDrawingAreaMakeCurrent(w, glxc);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
}
void
main(argc,argv)
int argc;
char **argv;
{
GLubyte *raster;
XtToolkitInitialize();
appContext = XtCreateApplicationContext();
display = XtOpenDisplay(appContext, NULL, "DefToO","defToO",NULL,0,
&argc,argv);
raster = defToO(test_width,test_height,test_raster);
testBitmap(raster);
}
/* defraster bitmap to openGL bitmap */
GLubyte *
defToO(w,h,r)
int w,h;
unsigned short *r;
{
GLubyte *xbm,*uc;
unsigned short *us;
int i,j;
int wide;
int tokensPerRow;
int outTokensPerRow;
width = w;
height = h;
if (width <= 16) {
tokensPerRow = 1;
}
else {
if (width%16 == 0) {
tokensPerRow = (int)(width / 16);
}
else {
tokensPerRow = (int)(width / 16) + 1;
}
}
if (width <= BITUNIT) {
outTokensPerRow = 1;
}
else {
if (width%BITUNIT == 0) {
outTokensPerRow = (int)(width / BITUNIT);
}
else {
outTokensPerRow = (int)(width / BITUNIT) + 1;
}
}
xbm = (GLubyte *)malloc(BITUNIT * outTokensPerRow * height);
us = r;
for (i=0;i<height;i++) { /* bottom top */
uc = xbm + i*outTokensPerRow;
wide = 0;
for (j=0;j<tokensPerRow;j++) {
*uc = (GLubyte)(*us >> 8);
uc++;
wide += 8;
if (wide < width) {
*uc = (GLubyte)(*us);
uc++;
wide += 8;
}
us++;
}
}
return(xbm);
}